import { default as coordinateUtils } from './coordinate.js'

export {
  loadCss,
  loadJs,
  isFunction,
  isArrayOfArrays,
  isArray,
  isObject,
  getRotation,
  coordinateUtils
}


/**
 * 加载css
 * @param {String} href
 */
function loadCss (href) {
  const node = document.createElement('link')
  node.rel = 'stylesheet'
  node.href = href
  document.getElementsByTagName('head')[0].appendChild(node)
}

/**
 * 加载js
 * @param {String} src
 */
function loadJs (src) {
  return new Promise((resolve) => {
    const script = document.createElement('script')
    script.type = 'text/javascript'
    script.async = false
    script.src = src
    document.getElementsByTagName('head')[0].appendChild(script)
    script.onload = function () {
      resolve()
    }
  })
}

function isFunction (v) {
  return typeof v === 'function'
}
function isArray (v) {
  return Array.isArray(v)
}
function isObject (obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}
function isArrayOfArrays (arr) {
  return Array.isArray(arr) && arr.every(v => Array.isArray(v))
}
/**
 * 获取选择的角度
 * @param {*} coordinates 
 * @returns 
 */
function getRotation (coordinates) {
  const [start, end] = coordinates || []
  const [startLng, startlat] = start || []
  const [endLng, endLat] = end || []
  const dx = endLng - startLng
  const dy = endLat - startlat
  const flag = dx >= 0
  return Math.atan2(flag ? dy : -dy, flag ? dx : -dx)
}